package cn.com.ddapp.ddm.ui.mine;

import android.app.Dialog;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.mine.UserData;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TimeCount;
import cn.com.ddapp.ddm.util.ToolFor9Ge;

public class OldAlterMobileActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.et_setmobile)
    EditText etSetmobile;
    @BindView(R.id.detele)
    ImageView detele;
    @BindView(R.id.bt_bind)
    Button btBind;

    TextView tvText;
    EditText et_verify;
    TextView getVerify;
    View layout;
    Button negativeBt1;
    Button positiveBt1;

    String userId;
    UserData userData;
    String oldMobile;
    String newMobile;
    TimeCount timeCount;
    Dialog dialog;
    int orientation;


    @Override
    public void initView() {
        titletext.setText(R.string.altermobile);
        etSetmobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if ("".equals(etSetmobile.getText().toString())){

                }else {
                    detele.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);
        oldMobile = getIntent().getStringExtra("mobile");
        LayoutInflater inflater = getLayoutInflater();
        layout = inflater.inflate(R.layout.dialog_mobile, (ViewGroup) findViewById(R.id.dialog_mobile));
        tvText = (TextView) layout.findViewById(R.id.tv_text);
        et_verify = (EditText) layout.findViewById(R.id.verify);
        getVerify = (TextView) layout.findViewById(R.id.getVerify);
        layout.setClickable(true);
        negativeBt1 = (Button) layout.findViewById(R.id.negative_bt1);
        positiveBt1 = (Button) layout.findViewById(R.id.positive_bt1);
        getVerify = (TextView) layout.findViewById(R.id.getVerify);
        negativeBt1.setClickable(true);
        positiveBt1.setClickable(true);
        et_verify.setClickable(true);
        getVerify.setClickable(true);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_old_alter_mobile;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.back,R.id.bt_bind,R.id.detele})
    public void onClick(View view){

        switch (view.getId()){
            case R.id.back :
                finish();
                break;

            case R.id.detele :
                etSetmobile.setText("");
                break;

            case R.id.bt_bind :
                final String mobile = etSetmobile.getText().toString();

                if ("".equals(mobile)){
                    BaseApplication.getIntstance().showToast("请输入手机号", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else if (!ToolFor9Ge.isPhoneNO(mobile)){
                    BaseApplication.getIntstance().showToast("请输入正确的电话号码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                }else {
                    if (dialog==null) {
                        dialog = new Dialog(this, R.style.FullScreenDialog);
                        dialog.setContentView(layout);
                        dialog.setCanceledOnTouchOutside(true);
                        negativeBt1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                                et_verify.setText("");
                                if (timeCount != null) {
                                    timeCount.cancel();
                                    timeCount.onFinish();
                                }

                            }
                        });
                        positiveBt1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if ("".equals(et_verify.getText().toString())) {
                                    BaseApplication.getIntstance().showToast("请输入验证码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                } else {
                                    OkUtilCallback callback = new OkUtilCallback() {
                                        @Override
                                        public void onMyResponse(String response, int id) {
                                            Gson gson = new Gson();
                                            Entity entity = gson.fromJson(response, Entity.class);
                                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                                BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                                tvText.setText(R.string.verify_error);
                                                tvText.setTextColor(getResources().getColor(R.color.color_orange));
                                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                                Intent i = new Intent(OldAlterMobileActivity.this, SetOkActivity.class);
                                                startActivity(i);
                                                finish();
                                                dialog.dismiss();
                                                timeCount.cancel();
                                            }
                                        }
                                    };
                                    OKUtilAPI.post(OKUtilAPI.updateMobile, "mobile=" + oldMobile + "&targetMobile=" + mobile + "&randCode=" + et_verify.getText().toString(), callback);
                                }
                            }
                        });
                        WindowManager m = getWindowManager();
                        Window dialogWindow = dialog.getWindow();
                        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高
                        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
                        orientation = getResources().getConfiguration().orientation;
                        if (orientation==0||orientation==2){
                            p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.6
                            p.width = (int) (d.getWidth() * 0.4); // 宽度设置为屏幕的0.65

                        }else {
                            p.height = (int) (d.getHeight() * 0.5); // 高度设置为屏幕的0.6
                            //p.width = (int) (d.getWidth() * 0.9); // 宽度设置为屏幕的0.65
                        }
                        dialogWindow.setAttributes(p);
                        dialog.show();
                    }else {
                        dialog.show();
                    }

/*                        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialog.dismiss();
                                et_verify.setText("");

                            }
                        });
                        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                if ("".equals(et_verify.getText().toString())){
                                    BaseApplication.getIntstance().showToast("请输入验证码", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                }else {
                                    OkUtilCallback callback = new OkUtilCallback() {
                                        @Override
                                        public void onMyResponse(String response, int id) {
                                            Gson gson = new Gson();
                                            Entity entity = gson.fromJson(response, Entity.class);
                                            Log.e("11111",userId);
                                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                                BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                                tvText.setText(R.string.verify_error);
                                                tvText.setTextColor(getResources().getColor(R.color.color_orange));
                                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                                Intent i = new Intent(OldAlterMobileActivity.this,SetOkActivity.class);
                                                startActivity(i);
                                                finish();
                                                dialog.dismiss();
                                                timeCount.cancel();
                                            }
                                        }
                                    };
                                    Log.e("1111","mobile="+oldMobile+"&targetMobile="+mobile+"&randCode="+et_verify.getText().toString());
                                    OKUtilAPI.post(OKUtilAPI.updateMobile,"mobile="+oldMobile+"&targetMobile="+mobile+"&randCode="+et_verify.getText().toString(),callback);
                                }
                            }
                        });
                        WindowManager m = getWindowManager();
                        Window dialogWindow = dialog.getWindow();
                        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
                        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
                        p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
                        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65
                        dialogWindow.setAttributes(p);
                        dialog.show();
                    }
                    else {
                        dialog.show();
                    }*/

                    tvText.setText(R.string.tv_text);
                    getVerify.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            OkUtilCallback callback = new OkUtilCallback() {
                                @Override
                                public void onMyResponse(String response, int id) {
                                    Gson gson = new Gson();
                                    Entity entity = gson.fromJson(response, Entity.class);
                                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                        BaseApplication.getIntstance().showToast("请检查网络", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                        BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                        BaseApplication.getIntstance().showToast("验证码已发送", Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                                        timeCount = new TimeCount(60000, 1000, getVerify);
                                        timeCount.start();
                                    }
                                }
                            };
                            OKUtilAPI.post(OKUtilAPI.sendVerifyCode, "key=" + mobile, callback);
                        }
                    });
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
